package com.offcn.dao.impl;

import com.offcn.dao.UserDao;
import com.offcn.entity.User;
import com.offcn.utils.BaseDao;
import com.offcn.utils.Page;

import java.util.List;

//这里一定要加泛型，指定父类中的是获取的那个类型的对象
public class UserDaoImpl extends BaseDao<User> implements UserDao {
    @Override
    public  User getUser(String username) {
        //调用工具类中的查询方法去查询，首先准备数据
        String sql = "select * from users where username=?";
        User user = (User) getBean(sql, username);
        return user;
    }

    @Override
    public void addUser(User user1) {
        //调用工具类中的update方法去添加数据
        String sql = "insert into users (id,username,password,email) values(0,?,?,?)";
        update(sql, user1.getUsername(), user1.getPassword(), user1.getEmail());
    }


    //首页分页展示
    @Override
    public Page<User> getUserList(Integer pageNo) {
        //创建page对象，将其中的属性全部封装进去
        Page<User> userPage = new Page<>();
        int totalRecord = totalRecord();
        String sql = "select * from users limit ?,?";
        List<User> userList = getBeanList(sql,(pageNo-1)*userPage.pageSize,userPage.pageSize);
        userPage.setTotalRecord(totalRecord);
        userPage.setPageNo(pageNo);
        userPage.setList(userList);
        return userPage;
    }

    @Override
    public int totalRecord() {
        String sql = "select count(*) from users";
        Long count = (Long) getSingleValue(sql);
        int totalRecord = count.intValue();
        return totalRecord;
    }

    //进行修改前的展示
    @Override
    public User getUserById(Integer sid) {
        String sql = "select * from users where id=?";
        User user = getBean(sql, sid);
        return user;
    }


    //数据修改
    @Override
    public void updateUser(User user) {
        String sql = "update users set username=?,password=?,email=? where id=?";
        update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getId());
    }

    //删除数据
    @Override
    public void deleteUserById(int sid) {
        String sql = "delete from users where id=?";
        update(sql,sid);
    }

/*
    public static void main(String[] args) {
        UserDaoImpl userDao = new UserDaoImpl();
//        Page<User> userList = userDao.getUserList(1);
//        for (User user : userList.getList()) {
//            System.out.println(user);
//        }

//        User user = userDao.getUserById(1);
//        System.out.println(user);
    }
*/


}
